<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * @param $data
 * @param $msg
 * @param int $status
 */
function ajaxReturn($data, $msg, $status=1){
    $info = array();
    $info['data'] = $data;
    $info['msg'] = $msg;
    $info['status'] = $status;
    if(sizeof($info)==0){
        header('Content-Type:text/html; charset=utf-8');
        exit(json_encode(array('error'=>"参数为空")));
    }else{
        header('Content-Type:text/html; charset=utf-8');
        exit(json_encode($info));
    }
}

/**
 * @param int $length
 * @return string
 */
function makeSalt( $length = 6 ){
    // 密码字符集，可任意添加你需要的字符
    $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
        'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
        't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D',
        'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O',
        'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    // 在 $chars 中随机取 $length 个数组元素键名
    $keys = array_rand($chars, $length);
    $password = '';
    for($i = 0; $i < $length; $i++)
    {
        // 将 $length 个数组元素连接成字符串
        $password .= $chars[$keys[$i]];
    }
    return str_shuffle(str_shuffle($password));
}


/**
 * base64图片存储
 * @param $base64_image_content  string base64格式
 * @param $path                  string 路径
 * @param $file_name             string 文件名
 * @return bool|string
 */
function base64_image_content($base64_image_content,$path,$file_name){
    //匹配出图片的格式
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
        $type = $result[2];
        $new_file = $path."/";
        $dir = iconv("UTF-8", "GBK", $new_file);
        if (!file_exists($dir)){
            mkdir ($dir,0777,true);
        }
        $file = $file_name.".{$type}";
        $new_file = $new_file.$file;
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
            return $file;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

/**
 * 替换1张图片 将 +rep+ 替换成图片
 * @param string $content
 * @param string $suffix
 * @return null|string|string[]
 */
function get_img_thumb_url($content = "",$suffix = "")
{
    $pregRule = '/\+rep\+/';
    $content = preg_replace($pregRule,"<img src=\"{$suffix}\" style=\"max-width:100%;\">",$content,1);
//    $content = preg_replace($pregRule,'$suffix, $content,1);
    return $content;
}

//删除单个文件方法
function del_file($path){
    if(file_exists($path)){
        return unlink($path);
    }
}

/**
 * 递归分类
 * @param $arr
 * @param int $pid
 * @param int $level
 * @return array
 */
function getTree($arr,$pid = 0,$level =1)
{
    $list =array();
    foreach ($arr as $k=>$v){
        if ($v['cate_pid'] == $pid){
            $v['level'] = $level;
            $v['child'] = getTree($arr,$v['cate_id'],$level+1);
            $list[] = $v;
        }
    }
    return $list;
}

function delete_dir_file($dir_name) {
    $result = false;
    if(is_dir($dir_name)){ //检查指定的文件是否是一个目录
        if ($handle = opendir($dir_name)) {   //打开目录读取内容
            while (false !== ($item = readdir($handle))) { //读取内容
                if ($item != '.' && $item != '..') {
                    if (is_dir($dir_name . DS . $item)) {
                        delete_dir_file($dir_name . DS . $item);
                    } else {
                        unlink($dir_name . DS . $item);  //删除文件
                    }
                }
            }
            closedir($handle);  //打开一个目录，读取它的内容，然后关闭
            if (rmdir($dir_name)) { //删除空白目录
                $result = true;
            }
        }
    }
    return $result;
}

